#!/bin/sh
#
# ssh -- login the docker lab via ssh
#
# Copyright (C) 2016-2020 Wu Zhangjin <lzufalcon@163.com>
#

uname | grep -q MINGW && PWD_OPT="-W"
TOP_DIR="$(cd "$(dirname "$0")"/../../ && pwd $PWD_OPT)"
. "$TOP_DIR/tools/docker/config" $* >/dev/null

which sshpass >/dev/null 2>&1
[ $? -eq 0 ] && SSH_PASS="sshpass -p '$UNIX_PWD'"

SSH_CMD="$SSH_PASS ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -t ${UNIX_USER}@${VNC_IP} -p ${CONTAINER_SSH_PORT} 'cd $LAB_WORKDIR/$CURRENT && exec bash -l'"
REMOTE_SSH_CMD="ssh  -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -t ${UNIX_USER}@${HOST} -p ${HOST_SSH_PORT} 'cd $LAB_WORKDIR/$CURRENT && exec bash -l'"

if [ -z "$SSH_PASS" ]; then
  echo
  echo "LOG: Password required, please login with the following command:"
  echo
  echo "  $SSH_CMD"
  echo

  echo "LOG: If want to login without password, there are two methods:"
  echo
  echo "  1. Install the 'sshpass' command"
  echo

  cat <<EOF
  2. Upload public ssh key with following steps:

        $ mkdir ~/.ssh/
        $ touch ~/.ssh/authorized_keys
        $ chmod 700 ~/.ssh/
        $ chmod 644 ~/.ssh/authorized_keys
        $ vim ~/.ssh/authorized_keys

        Generate a public ssh key with 'ssh-keygen'
        Copy your public ssh key and paste it into the ~/.ssh/authorized_key

EOF

fi

echo
echo "LOG: Running '$SSH_CMD'"
echo

eval "$SSH_CMD"
